layui.use(['table','form','jquery','element','upload'],function () {
    var form = layui.form,
        table = layui.table,
        element = layui.element,
        upload = layui.upload,
        $ = layui.jquery;


    /**
     * 图片上传
     */
    var uploadPhoto = upload.render({
        elem:'#photoImg',
        drag:true,
        url:'/reListController/upload',
        accept:'images',
        acceptMime: 'image/*',
        size: 0,
        before: function (obj) {
            //预读本地文件
            obj.preview(function (index, file, result) {
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        },
        done: function (res) {
            console.log(res);
            //如果上传失败
            if (res < 0) {
                return layer.msg('上传失败');
            }
            //上传成功
            var demoText = $('#demoText');
            demoText.html('<span style="color: green;">上传成功!</span>');
            $('input[name=photo]').val(res.data);
        },
        error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="line-height: 10px;color: #FF5722;">上传失败</span> <a class="demo-reload" style="line-height: 10px;color:darkblue;text-decoration:underline;">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadPhoto.upload();
            });
        }
        });
    element.init();

    /**
     * 新增报修单
     */
    form.on("submit(advise)",function (row) {
        var index = top.layer.msg('数据提交中...',{icon: 16,time:false,shade:0.8});
        var obj = row.field;
        console.log(obj);
        /**
         * 请求后台
         */
        $.ajax({
            type:'POST',
            url:'/reListController/add',
            data:JSON.stringify(obj),
            dataType:'json',
            contentType:'application/json',
            success:function (res) {
                // parent.location.reload();
                /**
                 * 添加成功后回调
                 **/
                setTimeout(function(){
                    top.layer.close(index);
                    top.layer.msg("申请报修成功");
                    $('input[name=userTel]').val('');
                    $('input[name=location]').val('');
                    $('textarea[name=detailInfo]').val('');
                    form.render();
                },2000);
                return false;
            }
        });
    })

    /**
     * 修改报修单
     */
    form.on("submit(updateRepair)",function (row) {
        var index = top.layer.msg('数据修改中，请稍候',{icon: 16,time:false,shade:0.8});
        var obj = row.field;
        console.log(obj);
        /**
         * 请求后台
         */
        $.ajax({
            type:'POST',
            url:'/reListController/update',
            data:JSON.stringify(obj),
            dataType:'json',
            contentType:'application/json',
            success:function (res) {
                /**
                 * 添加成功后回调
                 **/
                setTimeout(function(){
                    top.layer.close(index);
                    top.layer.msg("修改成功");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                },500);
                return false;
            }
        });
    })

    /**
    * 查看我的评论
     */
    form.on("submit(seeComment)",function (row) {
        var obj = row.field;
        console.log(obj);
        $.ajax({
            url: '/commentController/getCommentByReListId',
            type: 'GET',
            data: {reId: obj.id},
            success: function (res) {
                if (res.code === 200) {
                    var ele = "";
                    $.each(res.data, function (index, item) {
                        ele += " <div class=\"layui-row layui-col-space10\">\n" +
                            "        <span class=\"layui-col-xs10 layui-col-xs-offset1\">" + item.comments + "</span>\n" +
                            "    </div>";
                    });
                    $("#seeComments").append(ele);
                }
            }
        });
        layer.open({
            title: "评论列表",
            type: 1,
            area: ['38%', '45%'],
            content: $('#seeComments'),
            cancel: function (index, layero) {
                layer.close(index)
                $('#seeComments').remove();
                window.location.reload();
                return false;
            }
        });
    })

    /**
     * 查看我的评论
     */
    form.on("submit(seeReply)",function (row) {
        var obj = row.field;
        console.log(obj);

        $.ajax({
            url: '/replyController/getReplyByReListId',
            type: 'GET',
            data: {reId: obj.id},
            success: function (res) {
                if (res.code === 200) {
                    var ele = "";
                    ele += res.data.replyContent;
                    $("#seeReply").append(ele);
                }
            }
        })

        layer.open({
            title: "解决回复",
            type: 1,
            area: ['38%', '45%'],
            content: $('#seeReply'),
            cancel: function (index, layero) {
                layer.close(index)
                $('#seeReply').remove();
                window.location.reload();
                return false;
            }
        });
    })
});